#!/user/bin/env python3
# -*- coding: utf-8 -*-
import requests
import time
from urllib.parse import urljoin
from lxml import etree
import re
from database_api import MysqlClient
from setting import db_config

client = MysqlClient(db_config)  # mysql数据库连接
headers = {
        'authority': 'music.163.com',
        'method':'GET',
        'path':'/playlist?id=3172849329',
        'scheme':'https',
        'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-encoding':'gzip, deflate, br',
        'accept-language':'zh-CN,zh;q=0.9',
        'cookie':'_ntes_nuid=49967de6a918ad8de0e836abed23b9a9; mail_psc_fingerprint=840df7b10ed9a4e1b2944a99023acf6c; _iuqxldmzr_=32; WM_TID=uqv6Z1PiAClERBVQURZ9lSVqE4UUY8fK; UM_distinctid=16d5cfa4c07891-0456687a9c5ab1-67e1b3f-144000-16d5cfa4c08a57; nts_mail_user=zgc596918978@163.com:-1:1; _ntes_nnid=49967de6a918ad8de0e836abed23b9a9,1578536042971; vinfo_n_f_l_n3=a97f99debbb5cb4b.1.1.1569222970334.1569223029755.1578536082555; P_INFO=zgc596918978@163.com|1584345814|0|mail163|00&99|jis&1584059619&mail_client#jis&320500#10#0#0|&0|study_client&urs&mail163|zgc596918978@163.com; WM_NI=u92zX19t%2B2wQfD7ZnMMoePTFowQBiU3eQw%2FR5m%2FmF3b8UswhKl%2FpD6Bhiwq0XIg7BRPrtAwziQIxj87CsKfCMHms%2Bc2o2lrlOlBsBviAtXVEb2Kfe%2FvGkbk3Jv%2BIEx2UQlo%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6ee88d36bf795bdb8ca498def8ea3d85b928f8bbbaa4a90ad9b87c83df2bea6acec2af0fea7c3b92ab7e7fe9bdb3bfbace5d3f65ba69cf9bbed478fb1abb1d74eada99fa3ec5ff1bb9f98ae5af7b3e18ec539a2e89b94f66489b4e191c73e82b8a798f43c9c97a9d8c15a94aebca2f672ab8f8799e934f59dbdace5498f98a394aa69f2b1fe8df1688db48e94c17eab9fbcb9d54fa2ee8587e825b699a583ea398aaae5aac874f28b9f8dee37e2a3; __remember_me=true; ntes_kaola_ad=1; playerid=38364325; MUSIC_U=d01810611bab9af038338a157015adb2b933fb47cc4276a629b57f505548486ef33b7765a49aa807c28108a055d99099114f327788dd6fe3; __csrf=1e0a12e65ac10a5415c5a70fe216d23e; JSESSIONID-WYYY=Xi7ECBiX3XOhcHgytOJHPmxyfPaHS5FpXqYDAnqTuWiWevjeUw%2F82RP%2FcAHPRGTDn1mzQtwX1VteQOW0TEVD55giVTpYzD5uPy4rfhhmHhhDrMwcR9GBDQv2ZwKyGUpRoKJZxfZXrxwQcFvmu8EDS188IbO%2BEpYf7Q6WB%5C1w3X1%2FufHX%3A1584596757611',
        'referer':'https://music.163.com/',
        'sec-fetch-dest':'iframe',
        'sec-fetch-mode':'navigate',
        'sec-fetch-site':'same-origin',
        'upgrade-insecure-requests':'1',
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36',
    }

def parse(url):
    if re.findall("/#",url,re.S):
        url = url.replace("/#","")
    flag = True
    response = ""
    count = 1
    while flag:
        try:
            response = requests.get(url,headers=headers)
            if response.status_code == 200:
                flag = False
                break
        except Exception as e:
            print(e)
        count += 1
        if count > 4:
            flag = False
        time.sleep(10)
    if response:
        # print(response.text)
        html = etree.HTML(response.text)
        li_list = html.xpath("//ul[@class='f-hide']/li")
        for li in li_list:
            href = li.xpath("./a/@href")[0]   # 详情页的链接
            href = urljoin(url,href)
            parse_detail(href)
            time.sleep(1.5)

def parse_detail(url):
    if re.findall("id=(\d+)$",url,re.S):
        id = re.findall("id=(\d+)$",url,re.S)[0]
    else:
        id = ""
    flag = True
    response = ""
    count = 1
    while flag:
        try:
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                flag = False
                break
        except Exception as e:
            print(e)
        count += 1
        if count > 4:
            flag = False
        time.sleep(10)
    if response:
        try:
            html = etree.HTML(response.text)
            song_name = html.xpath("//div[@class='tit']/em/text()")[0]   # 歌曲名
            print(".........开始获取歌曲《%s》的数据"%song_name)
            singer = html.xpath("//div[@class='hd']/following-sibling::p[1]/span/@title")[0]   # 歌手
            album = html.xpath("//p[@class='des s-fc4']/a/text()")[0]   # 专辑
            img = html.xpath("//div[@class='u-cover u-cover-6 f-fl']/img/@src")[0]   # 歌曲图片
            if id:
                download_url = "http://music.163.com/song/media/outer/url?id=%s.mp3"%id
            else:
                download_url = ""
            print("待插入数据：",(song_name,singer,album,img,download_url))
            insert_sql = "insert into music(song_name,singer,album,img,download_url) values(%s)"%",".join(["'%s'"]*5)
            client.insert(insert_sql%(song_name,singer,album,img,download_url))
        except IndexError:
            print("会员歌曲，正在跳过....")
        except Exception as e:
            print(e)


if __name__ == '__main__':
    '''
    执行之前配置setting.py文件的数据库连接,sing_url为待爬取的歌单url
    '''
    sing_url = 'https://music.163.com/#/playlist?id=3232484836'
    # sing_url = "https://music.163.com/#/playlist?id=3172849329"
    parse(sing_url)